<template>
  <div class="app-container" ref="appContainer">
    <PropTable
      ref="tableData"
      :loading="loading"
      :columns="column"
      :data="data"
      :unadd="false"
      @reset="reset"
      @onSubmit="onSubmit"
    >
      <template v-slot:operation="scope">
        <el-button type="primary" size="small" icon="View" @click="viewPdf(scope.row)">
          预览
        </el-button>
        <el-button
          type="success"
          size="small"
          icon="CircleCheck"
          @click="ifPass('已通过', scope.row)"
          >通过</el-button
        >
        <el-button
          type="danger"
          size="small"
          icon="CircleClose"
          @click="ifPass('不通过', scope.row)"
        >
          不通过
        </el-button>
      </template>
    </PropTable>
    <ViewFileDrawer ref="fileViewDrawer" />
  </div>
</template>
<script lang="ts" setup name="comprehensive">
  import { ref, onMounted } from 'vue'
  import { ElMessage } from 'element-plus'
  import { getFileDetailById } from '@/api/controller/file-detail-controller/index'
  import {
    postFileApplicationSearch,
    putFileApplication,
  } from '@/api/controller/file-application-controller/index'
  import ViewFileDrawer from '@/components/fileViewDrawer/index.vue'
  const loading = ref(true)
  const appContainer = ref(null)
  import PropTable from '@/components/Table/PropTable/index.vue'

  const column = [
    { name: 'title', label: '文件标题', inSearch: true, valueType: 'input' },
    { name: 'type', label: '文件类型', inSearch: true, valueType: 'input', width: 140 },
    { name: 'stage', label: '文件分类', inSearch: true, valueType: 'input', width: 140 },
    { name: 'projectBrief', label: '项目简称', inSearch: true, valueType: 'input' },
    { name: 'status', label: '审核状态', valueType: 'input', width: 110 },
    { name: 'nickname', label: '申请人', inSearch: true, valueType: 'input', width: 110 },
    { name: 'operation', slot: true, fixed: 'right', width: 280, label: '操作', type: false },
  ]
  const data = ref(null)
  const ruleForm = ref({
    status: null,
    id: null,
  })
  const tableData = ref(null)
  //文件预览抽屉
  const fileViewDrawer = ref()
  const viewPdf = async (data) => {
    let res = await getFileDetailById({ id: data.id })
    fileViewDrawer.value.show(res.data)
  }

  //重置搜索条件
  const reset = async () => {
    try {
      loading.value = true
      let pageInfo = {
        pageNum: 1,
        pageSize: 15,
      }
      const result = await postFileApplicationSearch(pageInfo)
      data.value = result.data
    } finally {
      loading.value = false
    }
  }
  //搜索
  const onSubmit = async (val) => {
    try {
      loading.value = true
      const result = await postFileApplicationSearch(val)
      data.value = result.data
    } finally {
      loading.value = false
    }
  }
  //初始化
  onMounted(async () => {
    try {
      loading.value = true
      let pageInfo = {
        pageNum: 1,
        pageSize: 15,
      }
      const result = await postFileApplicationSearch(pageInfo)
      data.value = result.data
    } finally {
      loading.value = false
    }
  })
  //审核
  const ifPass = async (str, row) => {
    ruleForm.value.status = str
    ruleForm.value.id = row.id
    const result = await putFileApplication(ruleForm.value, { loading: true })
    if (result.code === 200) {
      try {
        loading.value = true
        const result = await postFileApplicationSearch(tableData.value?.formInline)
        data.value = result.data
        ElMessage.success('修改成功')
      } finally {
        loading.value = false
      }
    }
  }
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .app-container {
    flex: 1;
    display: flex;
    width: 100%;
    padding: 16px;
    box-sizing: border-box;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
